package gui.FileTransfer;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class FileTransferDialog extends JDialog implements ActionListener
{
	protected GridBagConstraints c = new GridBagConstraints();
	protected int numOfTasks = 0;
	protected ArrayList<FileTransferPanel> tasks = new ArrayList<FileTransferPanel>();
	
	public FileTransferDialog(Frame owner)
	{
		super(owner, false); // non-modal
		
		// set up the UI
		Container content = getContentPane();			
		GridBagLayout layout = new GridBagLayout();
		
		setLayout(layout);
		setTitle("File Transfers");
		pack();
	}
		
	public void actionPerformed(ActionEvent e)
	{
		
	}
	
	public int addFileTransferTask(String filename, int fileSize, FileTransferPanel.TRANSFER_MODES mode)
	{
		FileTransferPanel panel = new FileTransferPanel(filename, fileSize, mode);
		
		c.fill = GridBagConstraints.BOTH;
		c.gridy = numOfTasks;
				
		getContentPane().add(panel, c);
		tasks.add(panel);
		pack();
		
		return numOfTasks++;
	}
	
	public void updateFileTransferTask(String filename, int bytes)
	{
		for(int i=tasks.size()-1; i>=0; --i)
			if(tasks.get(i).filename.equals(filename))
			{
				updateFileTransferTask(i, bytes);
				break;
			} // IF
					
	}
	
	public void updateFileTransferTask(int taskID, int bytes)
	{
		tasks.get(taskID).setProgress(bytes);
		pack();
	}
}
